package demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import demo.model.Log;
import demo.model.User;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Path("/login")
@Clear
public class LoginController extends Controller {
    public void index() {
        render("login.html");
    }

    public void doLogin() {
        String username = getPara("username");
        String password = getPara("password");


        List<User> users = User.dao.find("select * from sys_user where user_name=? and " +
                "pass_word=?",username,password);
        if (users.size()>0) {
            Log log = new Log();
            log.setUserName(users.get(0).getUserName());
            log.setLoginTime(new Date());
            log.save();
            setSessionAttr("user",users.get(0));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "登录成功");
            renderJson(jsonObject);
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", -1);
            jsonObject.put("msg", "登录失败，账户或密码错误！");
            renderJson(jsonObject);
        }
    }



}
